package java8.lambda;

import java8.Employee;

import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 方法引用和构造器引用
 * 方法引用：
 * 1.对象::实例方法名
 * 2.类::静态方法名
 * 3.类::实例方法名
 *
 * 构造器引用：
 * 类::new
 *
 * 数组引用：
 * 类[]::new
 */
public class Lambda2 {

    public static void main(String[] args) {
        // 3种方法引用
        methodReference1();
        methodReference2();
        methodReference3();
        // 构造器引用
        methodReference4();
        // 数组引用
        methodReference5();
    }

    public static void methodReference1() {
        // 对象::实例方法名
        Consumer<String> consumer1 = s -> System.out.println(s);
        Consumer<String> consumer2 = System.out::println;

        Employee emp = new Employee();
        Supplier<String> supplier1 = () -> emp.getName();
        Supplier<String> supplier2 = emp::getName;
    }

    public static void methodReference2() {
        // 类::静态方法名
        Supplier<Double> supplier1 = () -> Math.random();
        Supplier<Double> supplier2 = Math::random;

        Comparator<Integer> comparator1 = (x, y) -> Integer.compare(x, y);
        Comparator<Integer> comparator2 = Integer::compare;
    }

    public static void methodReference3() {
        // 类::实例方法名（规则：第一个参数是实例方法的调用者，第二个参数是这个实例方法的参数）
        BiPredicate<String, String> biPredicate1 = (x, y) -> x.equals(y);
        BiPredicate<String, String> biPredicate2 = String::equals;
    }

    public static void methodReference4() {
        // 类::new
        Supplier<Employee> supplier1 = () -> new Employee();
        Supplier<Employee> supplier2 = Employee::new;
    }

    public static void methodReference5() {
        // 类[]::new
        Function<Integer, String[]> function1 = (x) -> new String[x];
        Function<Integer, String[]> function2 = String[]::new;
    }
}
